package com.loadagent.core.util;

import java.util.*;
import java.util.zip.*;
import java.io.*;

public class IoUtils {
    public static void writeFile(final File file, final byte[] fileBytes) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(fileBytes, 0, fileBytes.length);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(os);
        }
    }

    public static byte[] readFileToByte(final File file) {
        try {
            final FileInputStream inputStream = new FileInputStream(file);
            return toBytes(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] toBytes(final InputStream input) throws IOException {
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            final byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
            return output.toByteArray();
        } finally {
            close(output, input);
        }
    }

    public static void listFile(final List<File> fileList, final File dir, final String endWith) {
        if (!dir.exists()) {
            throw new IllegalArgumentException("\u76ee\u5f55[" + dir.getAbsolutePath() + "]\u4e0d\u5b58\u5728");
        }
        final File[] listFiles;
        final File[] files = listFiles = dir.listFiles();
        for (final File f : listFiles) {
            if (f.isDirectory()) {
                listFile(fileList, f, endWith);
            } else if (f.isFile() && f.getName().endsWith(endWith)) {
                fileList.add(f);
            }
        }
    }

    public static void listFile(final List<File> filess, final File dir) {
        if (!dir.exists()) {
            throw new IllegalArgumentException("\u76ee\u5f55[" + dir.getAbsolutePath() + "]\u4e0d\u5b58\u5728");
        }
        final File[] listFiles;
        final File[] files = listFiles = dir.listFiles();
        for (final File f : listFiles) {
            filess.add(f);
            if (f.isDirectory()) {
                listFile(filess, f);
            }
        }
    }

    public static void delete(final File dir) {
        if (!dir.exists()) {
            return;
        }
        if (dir.isFile()) {
            dir.delete();
        } else {
            final File[] listFiles;
            final File[] files = listFiles = dir.listFiles();
            for (final File f : listFiles) {
                delete(f);
            }
        }
        dir.delete();
    }

    public static int copy(final InputStream input, final OutputStream output) throws IOException {
        final byte[] buffer = new byte[4096];
        int count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    public static long crc32(final byte[] bytes) {
        final CRC32 crc = new CRC32();
        crc.update(bytes);
        return crc.getValue();
    }

    public static void close(final Closeable... outs) {
        if (outs != null) {
            for (final Closeable out : outs) {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static String readTxtFile(final File file) {
        final StringBuffer txt = new StringBuffer("");
        InputStreamReader read = null;
        BufferedReader bufferedReader = null;
        try {
            read = new InputStreamReader(new FileInputStream(file), "UTF-8");
            bufferedReader = new BufferedReader(read);
            String lineTxt;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                txt.append(lineTxt).append("\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(bufferedReader, read);
        }
        return txt.toString();
    }

    public static void writeTxtFile(final File file, final String txt) {
        BufferedWriter out = null;
        try {
            if (!file.exists()) {
                file.mkdirs();
                file.delete();
                file.createNewFile();
            }
            out = new BufferedWriter(new FileWriter(file));
            out.write(txt);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(out);
        }
    }

    public static byte[] merger(final byte[]... bts) {
        int lenght = 0;
        for (final byte[] b : bts) {
            lenght += b.length;
        }
        final byte[] bt = new byte[lenght];
        int lastLength = 0;
        for (final byte[] b2 : bts) {
            System.arraycopy(b2, 0, bt, lastLength, b2.length);
            lastLength += b2.length;
        }
        return bt;
    }
}
